<?php
/**
 * Created by : PhpStorm
 * User: 阿城 <479468212@qq.com>
 * Date: 2019/12/27
 * Time: 08:02
 */

namespace app\api\middleware;

use app\util\ReturnCode;

class Auth {

    /**
     * Token鉴权
     * @param \think\facade\Request $request
     * @param \Closure $next
     * @return mixed|\think\response\Json
     */

    public function handle($request, \Closure $next) {
        $loginToken = $request->header('App-Login-Token');
        if ($loginToken) {
            $userInfo = cache('Login:' . $loginToken);
            $userInfo = json_decode($userInfo, true);
            if (!$userInfo || !isset($userInfo['id'])) {
                return json([
                    'code' => ReturnCode::AUTH_ERROR,
                    'msg'  => '登录授权失败',
                    'data' => []
                ]);
            } else {
                $request->USER_INFO = $userInfo;
            }
            return $next($request);
        } else {
            return json([
                'code' => ReturnCode::AUTH_ERROR,
                'msg'  => '登录授权失败',
                'data' => []
            ]);
        }
    }
}
